import 'package:flutter/material.dart';
import './pages/tabs.dart';
import './pages/NameRouter/news.dart';
import './pages/NameRouter/form.dart';
import './pages/NameRouter/category.dart';
import './pages/NameRouter/shop.dart';

//ListView列表组件
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  //1.配置路由
  final Map _routers = {
    '/': (context) => MyTabs(),
    '/news': (context) => NewsPage(),
    '/form': (context, {arguments}) => FormPage(arguments: arguments),
    '/category': (context) => CategoryPage(),
    '/shop': (context, {arguments}) => ShopPage(arguments: arguments),
  };
  MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      // home: MyTabs(),
      initialRoute: '/',
      // 2.配置onGenerateRoute 固定写法
      onGenerateRoute: (RouteSettings settings) {
        print(settings);
        print(settings.name);
        print(settings.arguments);

        // 统一处理
        final String? name = settings.name; //news 或者是 form  -- 命名路由名称
        final Function? pageContentBuilder =
            _routers[name]; //(context) => MyTabs(),  router里面的Funtion

        if (pageContentBuilder != null) {
          if (settings.arguments != null) {
            final Route route = MaterialPageRoute(
              builder: (context) =>
                  pageContentBuilder(context, arguments: settings.arguments),
            );
            return route;
          } else {
            final Route route = MaterialPageRoute(
              builder: (context) => pageContentBuilder(context),
            );
            return route;
          }
        }
        return null;
      },
      // routes: {
      //   '/': (context) => MyTabs(),
      //   '/news': (context) => NewsPage(),
      //   '/form': (context) => FormPage(),
      //   '/category': (context) => CategoryPage(),
      // },
    );
  }
}
